﻿PuppetPlay.Puppet = function(config){
    Kinetic.Group.apply(this, [config]);

    var zoom = PuppetPlay.Util.getZoomFactor();
    var roundFn = PuppetPlay.Util.round;

    var rsGroup = PuppetPlay.Sources.Group[config.rsName];
    var rsControlPoint = PuppetPlay.Sources.ControlPoint[config.rsName];
    var rs = PuppetPlay.Sources.Images[config.rsName];

    var body = new PuppetPlay.Image(rs.body);   
    var larm = new PuppetPlay.Image(rs.larm);    
    var rarm = new PuppetPlay.Image(rs.rarm);    
    var lleg = new PuppetPlay.Image(rs.lleg);    
    var rleg = new PuppetPlay.Image(rs.rleg);

    var larmControlPoint = new Kinetic.Shape({
        x: roundFn(rsControlPoint.larmControlPoint.x * zoom),
        y: roundFn(rsControlPoint.larmControlPoint.y * zoom)
    });

    var rarmControlPoint = new Kinetic.Shape({
        x: roundFn(rsControlPoint.rarmControlPoint.x * zoom),
        y: roundFn(rsControlPoint.rarmControlPoint.y * zoom)
    });

    var bodyControlPoint = new Kinetic.Shape({
        x: roundFn(rsControlPoint.body.x * zoom),
        y: roundFn(rsControlPoint.body.y * zoom)
    });

    var leftArmGroup = new Kinetic.Group({ 
        x: roundFn(rsGroup.larmGroup.x * zoom),
        y: roundFn(rsGroup.larmGroup.y * zoom)
    });
    leftArmGroup.add(larm);
    leftArmGroup.add(larmControlPoint);
        
    var rightArmGroup = new Kinetic.Group({ 
        x: roundFn(rsGroup.rarmGroup.x * zoom),
        y: roundFn(rsGroup.rarmGroup.y * zoom)
    });
    rightArmGroup.add(rarm);
    rightArmGroup.add(rarmControlPoint);
    
    var leftlegGroup = new Kinetic.Group({ 
        x: roundFn(rsGroup.llegGroup.x * zoom),
        y: roundFn(rsGroup.llegGroup.y * zoom)
    });
    leftlegGroup.add(lleg);

    var rightlegGroup = new Kinetic.Group({ 
        x: roundFn(rsGroup.rlegGroup.x * zoom),
        y: roundFn(rsGroup.rlegGroup.y * zoom)
    });
    rightlegGroup.add(rleg);
    
    this.add(bodyControlPoint);

    if (config.face == "right") {
        this.add(leftArmGroup);
        this.add(leftlegGroup);
        this.add(body);
        this.add(rightlegGroup);
        this.add(rightArmGroup);
    }
    else if (config.face == "left") {
        this.add(rightArmGroup);
        this.add(rightlegGroup);
        this.add(body);       
        this.add(leftlegGroup);
        this.add(leftArmGroup);
    }

    /****************************Animation*********************************/
    //var animHasStart = false;
    //当前角度
    var currentLeftLegRotation = 0;
    var currentRightLegRotation = 0;
    //角度旋转增量
    var degrees = 3;
    //角度已旋量
    var changed = 0;
    //首先抬腿的脚
    var startleg = "left";
    //正/反转状态
    var alteredState = "foreward";
    //最大旋转角度
    var maxChange = 24;
    var anim = new Kinetic.Animation({
        func: function (frame) {
            if (alteredState == "foreward") {
                if (startleg == "left") {
                    leftlegGroup.setRotation(currentLeftLegRotation * (Math.PI / 180));
                    currentLeftLegRotation -= degrees;

                    rightlegGroup.setRotation(currentRightLegRotation * (Math.PI / 180));
                    currentRightLegRotation += degrees;
                }
                else if (startleg == "right")
                {
                    leftlegGroup.setRotation(currentLeftLegRotation * (Math.PI / 180));
                    currentLeftLegRotation += degrees;

                    rightlegGroup.setRotation(currentRightLegRotation * (Math.PI / 180));
                    currentRightLegRotation -= degrees;
                }
              
                changed += degrees;

                if (changed >= maxChange) {
                    alteredState = "reverse"                  
                }
            }
            else if (alteredState == "reverse")
            {
                if (startleg == "left") {
                    leftlegGroup.setRotation(currentLeftLegRotation * (Math.PI / 180));
                    currentLeftLegRotation += degrees;

                    rightlegGroup.setRotation(currentRightLegRotation * (Math.PI / 180));
                    currentRightLegRotation -= degrees;
                   
                }
                else if (startleg == "right") {
                    leftlegGroup.setRotation(currentLeftLegRotation * (Math.PI / 180));
                    currentLeftLegRotation -= degrees;

                    rightlegGroup.setRotation(currentRightLegRotation * (Math.PI / 180));
                    currentRightLegRotation += degrees;
                }
               
                changed -= degrees;

                if (changed <= 0) {

                    currentLeftLegRotation = 0;
                    currentRightLegRotation = 0;
                    changed = 0;

                    alteredState = "foreward"

                    if (startleg == "left") {
                        startleg = "right"
                    } else {
                        startleg = "left"
                    }

                    anim.stop();
                }
            }
        }
    });
    //anim.start();
    /****************************Animation*********************************/

    //this.add(new Kinetic.Shape({
    //    fill: 'rgb(0,0,255)',
    //    drawFunc: function (context) {
    //        context.beginPath();
    //        context.arc(0, 0, 12, 0, 2 * Math.PI , false);
    //        context.closePath();
    //        this.fill(context);
    //    }
    //}));

    this.bodyControlPoint = bodyControlPoint;
    this.body = body;
    this.leftArm = leftArmGroup;
    this.rightArm = rightArmGroup;
    this.leftleg = leftlegGroup;
    this.rightleg = rightlegGroup;
    this.anim = anim;
    //this.animHasStart = animHasStart;
};

PuppetPlay.Puppet.prototype = {
    rotateSpeedRatio: 0.02,
    rotateLeftArm: function (d) {
        document.getElementById("audioRotate").play()
	    //console.log('left rotate:'+this.leftArm.getRotation() / Math.PI * 180 + '('  + d + ',' + this.getLeftArmLength() + ')')
	    this.leftArm.rotate(d * this.rotateSpeedRatio);
    },
    rotateLeftHand: function(deg){
    },
    rotateRightArm: function (d) {
        document.getElementById("audioRotate").play()
        this.rightArm.rotate(d * this.rotateSpeedRatio);
    },
    rotateRightHand: function(deg){
    },

    getBodyJointPoint: function () {
        var pos = this.bodyControlPoint.getAbsolutePosition();
        var round = PuppetPlay.Util.round;
        return {
            x: round(pos.x),
            y: round(pos.y)
        };
    },

    getLeftArmJoinPoint: function () {
        //var zoomFactor = PuppetPlay.Util.getZoomFactor();
        //var joinPoint = PuppetPlay.Sources.ControlPoint[this.attrs.rsName].larmControlPoint;
        //var armCenter = this.leftArm.getAbsolutePosition();
        //var rotation = this.leftArm.getRotation();
        //var armLenth = this.getLeftArmLength();
        //var alpha = Math.atan(joinPoint.y / joinPoint.x); //initial angle
        ////console.log([(alpha + rotation) / Math.PI * 180, armLenth, Math.cos(rotation), Math.sin(rotation)])
        //return {
        //    x: armCenter.x + armLenth * Math.cos(alpha + rotation),
        //    y: armCenter.y + armLenth * Math.sin(alpha + rotation)
        //};

        var larmControlPointCenter = (this.leftArm.getChildren())[1].getAbsolutePosition();
        var round = PuppetPlay.Util.round;
        return {
            x: round(larmControlPointCenter.x),
            y: round(larmControlPointCenter.y)
        };
    },
    getRightArmJoinPoint: function () {
        //var zoomFactor = PuppetPlay.Util.getZoomFactor();
        //var joinPoint = PuppetPlay.Sources.ControlPoint[this.attrs.rsName].rarmControlPoint;
        //var armCenter = this.rightArm.getAbsolutePosition();
        //var rotation = this.rightArm.getRotation();
        //var armLenth = this.getRightArmLength();
        //var alpha = Math.atan(joinPoint.y / joinPoint.x); //initial angle
        
        ////console.log([(alpha + rotation) / Math.PI * 180, armLenth, Math.cos(rotation), Math.sin(rotation)])
        //return {
        //    x: armCenter.x +  armLenth * Math.cos(alpha + rotation),
        //    y: armCenter.y +  armLenth * Math.sin(alpha + rotation)
        //};
        var rarmControlPointCenter = (this.rightArm.getChildren())[1].getAbsolutePosition();
        var round = PuppetPlay.Util.round;
        return {
            x: round(rarmControlPointCenter.x),
            y: round(rarmControlPointCenter.y)
        };
    },
    rotate: function (pos) {
        document.getElementById("audioRotate").play()
        Kinetic.Group.prototype.rotate.call(this, pos);
    }
    ,
    move: function (pos) {
        Kinetic.Group.prototype.move.call(this, pos);
        //if (this.animHasStart == false) {
        //    this.anim.start();
        //    this.animHasStart == true;
        //}
        document.getElementById("audioWalk").play()
        this.anim.start();
        
    }
    //getLeftArmLength: function () {
    //    var zoomFactor = PuppetPlay.Util.getZoomFactor();
    //    var joinPoint = PuppetPlay.Sources.ControlPoint[this.attrs.rsName].larmControlPoint;
    //    return Math.sqrt(Math.pow(joinPoint.x * zoomFactor, 2) + Math.pow(joinPoint.y * zoomFactor, 2));
    //},

    //getRightArmLength: function () {
    //    var zoomFactor = PuppetPlay.Util.getZoomFactor();
    //    var joinPoint = PuppetPlay.Sources.ControlPoint[this.attrs.rsName].rarmControlPoint;
    //    return Math.sqrt(Math.pow(joinPoint.x * zoomFactor, 2) + Math.pow(joinPoint.y * zoomFactor, 2));
    //}
};

Kinetic.Global.extend(PuppetPlay.Puppet, Kinetic.Group);